using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Units;
using SolanumSolstice.Mapping;

namespace SolanumSolstice.Buildings
{


    public class Trap
    {
        protected Texture2D texture;
        protected Vector2 position;
        protected bool isConstructed;
        protected bool isAcceptable;
        protected bool isDead;
        protected int width;
        protected int height;
        protected int woodCost;

        protected int maxUses;
        protected int useCount;

        protected int frameCount;

        protected ContentManager content;

        #region Accessors
        public int Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }

        public int Width
        {
            get
            {
                return width;
            }
            set
            {
                width = value;
            }
        }

        public Vector2 Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public bool IsDead
        {
            get
            {
                return isDead;
            }
            set
            {
                isDead = value;
            }
        }

        public bool IsConstructed
        {
            get
            {
                return isConstructed;
            }
            set
            {
                isConstructed = value;
            }
        }

        public bool IsAcceptable
        {
            get
            {
                return isAcceptable;
            }
            set
            {
                isAcceptable = value;
            }
        }

        public int WoodCost
        {
            get
            {
                return woodCost;
            }
            set
            {
                woodCost = value;
            }
        }

        public int MaxUses
        {
            get
            {
                return maxUses;
            }
            set
            {
                maxUses = value;
            }
        }

        public string Content { set { texture = content.Load<Texture2D>(value); } }

        public int UseCount
        {
            get
            {
                return useCount;
            }
            set
            {
                useCount = value;

                if (useCount <= 0)
                {
                    useCount = 0; IsDead = true;
                }
            }
        }

        #endregion

        public Trap(ContentManager cont, Vector2 position)
        {
            content = cont;

            this.position = position;
            width = 32;
            height = 32;
            isConstructed = false;
            Content = "Content\\Txtr\\Bldg\\trap_spikes";
            woodCost = 15;
            maxUses = 5;
            frameCount = 0;
        }

        public void Update(GameTime gameTime, GamePadState gamePad, GameCursor gameCursor)
        {
            if (isConstructed == true)
            {

            }
            else
            {
                int modX = gameCursor.ScreenPositionX % 32;
                int modY = gameCursor.ScreenPositionY % 32;

                position = Map.GetMapPosition(new Vector2(gameCursor.ScreenPositionX - modX, (int)gameCursor.ScreenPositionY - modY));
                isAcceptable = SolanumSolstice.RoadTrapIsAcceptable(this);
            }
        }

        public void Draw(SpriteBatch spritebatch, int mapFocusX, int mapFocusY)
        {

            if (isConstructed)
            {
                frameCount++;

                if (frameCount > 39)
                {
                    frameCount = 0;
                }
                spritebatch.Draw(texture,
                    new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY, width, height), new Rectangle(32 *(frameCount / 10), 0, width, height),
                     Color.White);
            }
            else
            {
                Color colorAcceptable = Color.Green;

                if (isAcceptable == false)
                {
                    colorAcceptable = Color.Red;
                }

                spritebatch.Draw(texture,
                    new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY, width, height), colorAcceptable);



            }
        }
    }
}

